﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AutoSubmit.Lib;
using System.Reflection;

namespace Flying.AutoSubmit
{
    public class ActionRunner
    {
        public void Run(string runDir)
        {
            
            log4net.ILog log = log4net.LogManager.GetLogger("AutoSubmit");

            //read all configuration files under specific folder
            DirectoryInfo runFolder = new DirectoryInfo(runDir);
            FileInfo[] configurations = runFolder.GetFiles("*.config.txt");
            foreach (FileInfo configuration in configurations)
            {
                ActionPage page = null;
                try
                {
                    ConfigHelper configHelper = new ConfigHelper(configuration.FullName);
                    //fileName
                    string fileName = configuration.Name;
                    fileName = fileName.Substring(0, fileName.IndexOf(".config.txt"));

                    log.Info("Start Auto Submit to " + fileName);

                    // get the assembly
                    Type actionPageType = typeof(ActionPage);
                    Assembly dynamicAssembly = actionPageType.Assembly;
                   // Assembly dynamicAssembly = Assembly.LoadFile(@"D:\Workspace\AutoSubmit_Refact\AutoSubmit\AutoSubmit\bin\Debug\AutoSubmit.Lib.dll");
                    Type t = dynamicAssembly.GetType("AutoSubmit.Lib." + fileName + "." + fileName);
                    page = Activator.CreateInstance(t) as ActionPage;

                    // assembly the login
                    Login login = new Login();
                    login.m_loginURL = configHelper.GetValueByKey("login.URL");
                    login.m_userName = configHelper.GetValueByKey("login.userName");
                    login.m_userNameXPath = configHelper.GetValueByKey("login.userNameXPath");
                    login.m_password = configHelper.GetValueByKey("login.password");
                    login.m_passwordXPath = configHelper.GetValueByKey("login.passwordXPath");
                    login.m_submitXPath = configHelper.GetValueByKey("login.submitXPath");
                    if (configHelper.GetValueByKey("login.isClick").Length > 0)
                    {
                        login.m_isClick = Boolean.Parse(configHelper.GetValueByKey("login.isClick"));
                    }
                    
                    page.m_login = login;

                    log.Info("Login Page: " + login.m_loginURL);
                    log.Info("Login Name: " + login.m_userName);
                    log.Info("Password: " + login.m_password);

                    // assembly the submit
                    if ( ! configHelper.ContainsKey("submit.articlePath"))
                    {
                        throw new Exception(fileName + ": cannot find local article file!");
                    }
                    ArticleHelper ah = new ArticleHelper(runFolder.FullName + "\\" + configHelper.GetValueByKey("submit.articlePath"));

                    Submit submit = new Submit();
                    submit.m_url = configHelper.GetValueByKey("submit.URL");

                    submit.m_titleXPath = configHelper.GetValueByKey("submit.title");
                    submit.m_title = ah.GetValueByKey("submit.title");

                    submit.m_summaryXPath = configHelper.GetValueByKey("submit.summary");
                    submit.m_summary = ah.GetValueByKey("submit.summary");

                    submit.m_submitButtonXPath = configHelper.GetValueByKey("submit.submitButton");

                    submit.m_subCatagoryXPath = configHelper.GetValueByKey("submit.subCatagoryXPath");
                    submit.m_subCatagory = configHelper.GetValueByKey("submit.subCatagory");

                    submit.m_mainCategoryXPath = configHelper.GetValueByKey("submit.mainCategoryXPath");
                    submit.m_mainCategory = configHelper.GetValueByKey("submit.mainCategory");

                    submit.m_keywordsXPath = configHelper.GetValueByKey("submit.keywords");
                    submit.m_keywords = ah.GetValueByKey("submit.keywords");

                    submit.m_isCategorySelect = configHelper.ContainsKey("submit.isCategorySelect") ? Boolean.Parse(configHelper.GetValueByKey("submit.isCategorySelect")) : true;
                    submit.m_errBoxXPath = configHelper.GetValueByKey("submit.errBox");

                    submit.m_bodyXPath = configHelper.GetValueByKey("submit.body");
                    submit.m_body = ah.GetValueByKey("submit.body");

                    submit.m_bioXPath = configHelper.GetValueByKey("submit.bio");
                    submit.m_bio = ah.GetValueByKey("submit.bio");

                    submit.m_bioPlainXPath = configHelper.GetValueByKey("submit.bioPlain");
                    submit.m_bioPlain = ah.GetValueByKey("submit.bioPlain");

                    submit.m_authorXPath = configHelper.GetValueByKey("submit.author");
                    submit.m_author = ah.GetValueByKey("submit.author");

                    submit.m_webURLXPath = configHelper.GetValueByKey("submit.webURL");
                    submit.m_webURL = ah.GetValueByKey("submit.webURL");

                    submit.m_primarySearchPhraseXPath = configHelper.GetValueByKey("submit.primarySearchPhrase");
                    submit.m_primarySearchPhrase = ah.GetValueByKey("submit.primarySearchPhrase");

                    submit.m_tosXPath = configHelper.GetValueByKey("submit.tos");

                    if (configHelper.GetValueByKey("submit.isClick").Length > 0)
                    {
                        submit.m_isClick = Boolean.Parse(configHelper.GetValueByKey("submit.isClick"));
                    }

                    page.m_submit = submit;

                    log.Info("Submit Page: " + submit.m_url);
                    log.Info("Category: " + submit.m_mainCategory + " / " + submit.m_subCatagory);
                    log.Info("Article: " + submit.m_body);

                    // assembly the article
                    Article art = new Article();
                    art.m_url = configHelper.GetValueByKey("article.URL");
                    art.m_articlesListXPath = configHelper.GetValueByKey("article.listXPath");
                    art.m_title = ah.GetValueByKey("submit.title");

                    page.m_article = art;

                    log.Info("Article List Page: " + art.m_url);
                    log.Info("Starting login......");
                    if (!page.Login())
                    {
                        throw new Exception(fileName + " Login Error: " + page.GetErrMessage());
                    }

                    log.Info("Starting submit......");
                    if (!page.Submit())
                    {
                        throw new Exception(fileName + " Submit Error: " + page.GetErrMessage());
                    }

                    log.Info("Starting get article URL......");
                    String articleURL = page.GetArticleURL();
                    if (articleURL == null || articleURL.Length == 0)
                    {
                        throw new Exception(fileName + " Get Article URL Error: " + page.GetErrMessage());
                    }

                    log.Info("New Article URL: " + articleURL);
                }
                catch (System.Exception ex)
                {
                    // TODO: Log the error info
                    log.Error(ex.Message);
                    //Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (page != null)
                    {
                        page.m_driver.Quit();
                    }
                }
                
            }
        }
    }
}
